<?php

namespace app\admin\controller;

use app\admin\constants\AdminConstants;
use app\common\controller\Backend;
use Throwable;

/**
 * 房间信息管理
 */
class Room extends Backend
{
    /**
     * Room模型对象
     * @var object
     * @phpstan-var \app\admin\model\Room
     */
    protected object $model;
    protected bool|string|int $dataLimit = false;
    protected bool $storeldAutoFill = true;
    protected array|string $preExcludeFields = ['id', 'created_at', 'updated_at', 'external_id'];
    protected bool $modelSceneValidate = true;
    protected string|array $quickSearchField = ['id', 'name'];

    protected array $noNeedLogin = ['apiRooms'];
    protected array $noNeedPermission = ['apiRooms'];

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new \app\admin\model\Room();
    }
    /**
     * 查看
     * @throws Throwable
     */
    public function apiRooms(): void
    {
        $api_token = $this->request->header('token') ?: $this->request->param('token', '');
        if ($api_token != AdminConstants::API_TOKEN) {
            $this->error('token错误');
        }
        if (empty($this->request->store_id)) {
            $this->error('没有store_id参数！');
        }

        list($where, $alias, $limit, $order) = $this->queryBuilder();
        $where[] = $this->buildStoreWhere('room.store_id');
        $res = $this->model
            ->field('id,name,store_id')
            ->withJoin($this->withJoinTable, $this->withJoinType)
            ->alias($alias)
            ->where($where)
            ->order($order)
            ->paginate($limit);

        $this->success('', [
            'list'   => $res->items(),
            'total'  => $res->total(),
            'remark' => get_route_remark(),
        ]);
    }
    /**
     * 查看
     * @throws Throwable
     */
    public function index(): void
    {
        if ($this->request->param('select')) {
            $this->select();
        }

        list($where, $alias, $limit, $order) = $this->queryBuilder();
        $where[] = $this->buildStoreWhere('room.store_id');
        $res = $this->model
            ->field($this->indexField)
            ->withJoin($this->withJoinTable, $this->withJoinType)
            ->alias($alias)
            ->where($where)
            ->order($order)
            ->paginate($limit);

        $this->success('', [
            'list'   => $res->items(),
            'total'  => $res->total(),
            'remark' => get_route_remark(),
        ]);
    }
}
